package com.tensquare.search.service;

import com.tensquare.search.dao.SearchDao;
import com.tensquare.search.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import util.IdWorker;

/**
 * Created with IDEA
 * author:zhiXiang-Chen
 * Date:2019/3/2
 * Time:20:29
 */
@Service
public class SearchService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private SearchDao searchDao;

    public void save(Article article) {
        article.setId(new IdWorker().nextId()+"");
        searchDao.save(article);
    }

    public Page<Article> search(String keywords, int page, int size) {

        Pageable pageable = PageRequest.of(page-1,size);
        Page<Article> articlePage = searchDao.findByTitleLikeOrContentLike(keywords, keywords, pageable);
        return articlePage;
    }
}
